//
//  UIViewController+ActionSheet.swift
//  mall-ios
//
//  Created by Meak Tsui on 2018/12/5.
//  Copyright © 2018 Meak Tsui. All rights reserved.
//

import Foundation
import UIKit

extension UIViewController {
    public func alert(title:String?, message:String?,callback: (() -> ())? = nil) {
        let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: NSLocalizedString("好", comment: "好"), style: .default) { (action) in
            callback?()
        }
        controller.addAction(action)
        self.present(controller, animated: true, completion: nil)
    }
    
    public func confirm(title:String?, message:String?,yesTitle:String? = nil,noTitle:String? = nil,callback: ((_ result:Bool) -> ())? = nil) {
        let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let yesAction = UIAlertAction(title: yesTitle ?? NSLocalizedString("确定", comment: "确定"), style: .default) { (action) in
            callback?(true)
        }
        let noAction = UIAlertAction(title: noTitle ?? NSLocalizedString("取消", comment: "取消"), style: .default) { (action) in
            callback?(false)
        }
        controller.addAction(noAction)
        controller.addAction(yesAction)
        self.present(controller, animated: true, completion: nil)
    }
    
    public func actionSheet(title:String? = nil,message:String? = nil,actions: UIAlertAction... ) {
        let controller = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
        for action in actions {
            controller.addAction(action)
        }
        self.present(controller, animated: true, completion: nil)
    }
}
